package com.jzr.medical.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.medical.common.LayServiceImpl;
import com.jzr.medical.db1.mapper.TSysJobLogMapper;
import com.jzr.medical.db1.mapper.TSysJobMapper;
import com.jzr.medical.db1.model.TSysJob;
import com.jzr.medical.db1.model.TSysJobLog;
import com.jzr.medical.db1.service.SysJobService;
import com.jzr.medical.db1.vo.SysJobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysJobServiceImpl extends LayServiceImpl<TSysJobMapper, TSysJob,SysJobVo,Integer> implements SysJobService {
    @Autowired
    private TSysJobLogMapper sysJobLogMapper;

    @Override
    public SysJobVo saveAndGet(SysJobVo record) {
        setDefaultValue(record);

        if(record == null
                || (record.getId() == null
                    && record.getJobName() == null
                    && record.getCron() == null
                    && record.getJobClass() == null
                    && record.getStartDate() == null
                    && record.getStopDate() == null)){
            return null;
        }
        int count = 0;
        if(record.getId() == null ){
            count = baseMapper.insert(record);
        }else{
            count = baseMapper.insert(record);
        }
        if(count == 0){
            return null;
        }else{
            return getByIdV(record.getId());
        }
    }
    @Override
    public SysJobVo updateAndGet(SysJobVo record) {
        return saveAndGet(record);
    }
    @Override
    public String getErrLog(Integer id) {
        if(id == null){
            return "";
        }
        QueryWrapper<TSysJobLog> qw = new QueryWrapper<>();
        qw.select("job_log");
        qw.eq(TSysJobLog.JobId_,id);
        List<TSysJobLog> list = sysJobLogMapper.selectList(qw);
        if(list == null || list.size()<=0){
            return "";
        }else{
            return list.get(0).getJobLog();
        }
    }
}
